package com.blrs.service.list.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.uuid.UUID;
import com.blrs.domain.ProductList;
import com.blrs.mapper.ProductListMapper;
import com.blrs.mapper.QualificationSwpplierMapper;
import com.blrs.service.list.IProductListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 【产品清单】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-22
 */
@Service
public class ProductListServiceImpl implements IProductListService
{
    @Autowired
    private ProductListMapper productListMapper;
    @Autowired
    private QualificationSwpplierMapper qualificationSwpplierMapper;

    /**
     * 查询【产品清单】
     * 
     * @param id 【产品清单】主键
     * @return 【产品清单】
     */
    @Override
    public ProductList selectProductListById(String id)
    {
        return productListMapper.selectProductListById(id);
    }

    /**
     * 查询【产品清单】列表
     * 
     * @param productList 【产品清单】
     * @return 【产品清单】
     */
    @Override
    public List<ProductList> selectProductListList(ProductList productList)
    {
        return productListMapper.selectProductListList(productList);
    }

    /**
     * 新增【产品清单】
     * 
     * @param productList 【产品清单】
     * @return 结果
     */
    @Override
    public int insertProductList(ProductList productList)
    {
        productList.setId(UUID.randomUUID().toString());
        productList.setCreateId(SecurityUtils.getUserId());
        productList.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName());
        productList.setCreateTime(new Timestamp(new Date().getTime()));
        productList.setOrganizationId(SecurityUtils.getOrgId());
        productList.setOrganizationName(SecurityUtils.getOrgName());
        productList.setCreationTime(new Timestamp(new Date().getTime()));
        return productListMapper.insertProductList(productList);
    }

    /**
     * 修改【产品清单】
     * 
     * @param productList 【产品清单】
     * @return 结果
     */
    @Override
    public int updateProductList(ProductList productList)
    {
        return productListMapper.updateProductList(productList);
    }

    /**
     * 批量删除【产品清单】
     * 
     * @param ids 需要删除的【产品清单】主键
     * @return 结果
     */
    @Override
    public int deleteProductListByIds(String[] ids)
    {
        //判断当前主营产品是否关联了这个产品清单，如果有，则不能删除
        if (qualificationSwpplierMapper.selectQualificationSwpplierCountBySupplierId(ids[0])>0){
          throw new RuntimeException("当前主营产品已关联了这个产品清单，不能删除");
        }
        return productListMapper.deleteProductListByIds(ids);
    }

    /**
     * 删除【产品清单】信息
     * 
     * @param id 【产品清单】主键
     * @return 结果
     */
    @Override
    public int deleteProductListById(String id)
    {
        return productListMapper.deleteProductListById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importExcel(List<ProductList> list, String enterpriseId) {
        for (ProductList productList : list) {
            productList.setId(UUID.randomUUID().toString());
            productList.setCreateId(SecurityUtils.getUserId());
            productList.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName());
            productList.setCreateTime(new Timestamp(new Date().getTime()));
            productList.setOrganizationId(SecurityUtils.getOrgId());
            productList.setOrganizationName(SecurityUtils.getOrgName());
            productList.setCreationTime(new Timestamp(new Date().getTime()));
            productList.setEnterpriseId(enterpriseId);
            productListMapper.insertProductList(productList);
        }
        return "success";
    }

    @Override
    public List<ProductList> selectProductListListProductInfo(ProductList productList) {
        productList.setOrganizationId(SecurityUtils.getOrgId());
       return productListMapper.selectProductListListProductInfo(productList);
    }

//    @Override
//    public List<ProductList> selectProductListListSupplierCount(ProductList productList) {
//        return productListMapper.selectProductListListSupplierCount(productList);
//    }
}
